/**
 * 
 */
package es.uab.es2.TimeTracker.iuAndroid;

import java.util.ArrayList;
import java.util.Iterator;

import es.uab.es2.TimeTracker.nucli.Activitat;
import es.uab.es2.TimeTracker.nucli.Projecte;
import es.uab.es2.TimeTracker.nucli.Tasca;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import android.widget.Toast;



/**
 * @author sule
 *
 */
public class CrearActivity extends Activity {

    
    
  
    /**
     * 
     */
    private Projecte  projecteP;
    /**
     * 
     */
    private int posicio;
    /**
     * 
     */
    private Boolean p = false, t = false;
    /**
     * 
     */
    private Button bCrear, bModificar;
    /**
     * 
     */
    private RadioButton rProjecte, rTasca;
    /**
     * 
     */
    private EditText tNom, tDesc;
    /**
     * 
     */
    private String nom, descripcio, bNom;
    /**
     * 
     */
    private Projecte tArrel;
    /**
     * (non-Javadoc).
     * @see android.app.Activity#onCreate(android.os.Bundle).
     * @param savedInstanceState hola.
     */
    public final void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.crear_modificar);
        Bundle d = this.getIntent().getExtras();
        @SuppressWarnings("unchecked")
        ArrayList<DadesActivitat> pr = 
            (ArrayList<DadesActivitat>) d.get("origen");
         posicio = d.getInt("index");
         tArrel = GestorArbreActivitats.getArrel();
        
        
     
      bNom = pr.get(posicio).getNom();
      buscarProjecte(GestorArbreActivitats.getArrel(), bNom);
      bCrear = (Button) findViewById(R.id.crearActivitat);
      bModificar = (Button) findViewById(R.id.modificarActivitat);
      tNom = (EditText) findViewById(R.id.nom);
      tDesc = (EditText) findViewById(R.id.descripcio);
      rProjecte = (RadioButton) findViewById(R.id.radio_proj);
      rTasca = (RadioButton) findViewById(R.id.radio_tasc);

     
      bCrear.setOnClickListener(new View.OnClickListener() {
          public void onClick(final View v) {
              // Perform action on click
              Button b = (Button) v;
              if (b.getId() == bCrear.getId()) {
                 nom = tNom.getText().toString();
                 
                 descripcio = tDesc.getText().toString();
                 if (p) {
                     nom = "P" + "\t" + nom;
                     new Projecte(nom, descripcio, projecteP);
                    
                 } else if (t) {
                     nom = "T" + "\t" + nom;
                     new Tasca(nom, descripcio, projecteP);
                 }
                 
                 Toast.makeText(CrearActivity.this, "Activitat realitzada",
                         Toast.LENGTH_SHORT).show();
               
                 CrearActivity.this.finish();
               
              } else if (b.getId() == bModificar.getId()) {
                  Toast.makeText(CrearActivity.this, "No fa res",
                          Toast.LENGTH_SHORT).show();
                  this.notifyAll();
                
              }
          }
      });
     

      
    }
    /**
     * @param v vista amb la que treballem.
     */
    public final void onRadioButtonClicked(final View v) {
        // Perform action on clicks
        //Este codigo lo he copiado pero no se bien lo que hace
        //pero el evento del radiobutton es este.
        RadioButton rb = (RadioButton) v;
       if (rb.getId() == rProjecte.getId()) {
           p = true;
           t = false;
           Toast.makeText(CrearActivity.this,
                   "Projecte", Toast.LENGTH_SHORT).show();
       } else if (rb.getId() == rTasca.getId()) {
           t = true;
           p = false;
           Toast.makeText(CrearActivity.this, 
                   "Tasca", Toast.LENGTH_SHORT).show();
       }
        
    }
  
 
    /**
     * 
     * @param arrel arrel.
     * @param bnom nom a buscar.
     */
    public final void buscarProjecte(final Projecte arrel, final String bnom) {
        
        Iterator<Activitat> iter = arrel.getActivitats().iterator();
        
        while (iter.hasNext()) {
           Activitat act = iter.next();
           if (act.getClass() == Projecte.class) {
               Projecte tProj = (Projecte) act;
               if (bnom.equals(tProj.getNom())) {
                  projecteP = tProj;
               } else if (bnom != tProj.getNom()) {
                   buscarProjecte(tProj, bnom);
               }
               
           }
            
        }
        
        
    }
}
